package com.edu.test;

import com.edu.bean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Scanner;

/*
 * @Author:mx
 * @Date:2025/11/4-16:22:55
 */
public class MyTest02 {
    /*
     *   如何 获取 一个 Class类型的对象
     *       类名.class;
     *
     *       对象.getClass();
     *
     *       Class.forName("类完整限定名")
     *
     *
     * */
    @Test
    public void test01() throws ClassNotFoundException {
        Class clazz = User.class;
        System.out.println(clazz);

        User user = new User();
        Class clazz2 = user.getClass();
        System.out.println(clazz2);

        Class clazz3 = Class.forName("com.edu.bean.User");
        System.out.println(clazz == clazz2);
        System.out.println(clazz2 == clazz3);
        System.out.println(clazz3 == clazz);
    }
//
//
//    @Test
//    public void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//      Scanner sc = new Scanner(System.in);
//      String cName = sc.next();
//      Class clazz = Class.forName(cName);
//      Object o = clazz.newInstance();
//      System.out.println(o);
//    }
//
//    public static void main(String[] args) throws  Exception{
//        Scanner sc = new Scanner(System.in);
//        String cName = sc.next();
//        Class clazz = Class.forName(cName);
//        Object o = clazz.newInstance();
//        System.out.println(o);
//    }

    /*
     *   Class类型的对象常用的一些方法
     * */
    @Test
    public void test() throws InstantiationException, IllegalAccessException {
        Class clazz = User.class;
        System.out.println(clazz.getName());//完整限定名获取
        System.out.println(clazz.getSimpleName());//获取简单类名
        System.out.println(clazz.getSuperclass());//获取 该类型的 父类的类型
        System.out.println(clazz.getInterfaces());// 获取其实现的接口
        Class[] classes = clazz.getInterfaces();
        for (Class aClass : classes) {
            System.out.println(aClass.getName());
        }
        String str = "hello";
        System.out.println(clazz.getClassLoader());//获取其类加载器
        System.out.println(ClassPathXmlApplicationContext.class.getClassLoader());
        System.out.println(str.getClass().getClassLoader());;
        Object o = clazz.newInstance();
        System.out.println(o);

    }






}
